

class TimeUtil{
  /// 时间格式
  /// @param time 原数
  static String timeFormat(String timeStr){
    String result;
    DateTime time = DateTime.parse(timeStr);
    DateTime now = DateTime.now();
    if(now.year - time.year < 1){//时间差在一年内
      Duration duration = now.difference(time);
      if(duration.inSeconds < 60){
        result = "刚刚";
      } else if(duration.inMinutes < 60){
        result = "${duration.inMinutes}分钟前";
      } else if(duration.inHours < 24 && duration.inDays == 0){
        result = "${duration.inHours}小时前";
      } else if(duration.inDays == 1){
        result = "昨天";
      } else if(duration.inDays == 2){
        result = "前天";
      }else {
        if(time.month > 9) {
          result = "${time.month}-${time.day} ${time.minute}:${time.second}";
        } else {
          result = "0${time.month}-${time.day} ${time.minute}:${time.second}";
        }
      }
    } else {
      result = timeStr;
    }
    return result;
  }

  /// 是否今天
  /// @param time 原数
  static bool isToday(String timeStr){
    bool result = false;
    DateTime time = DateTime.parse(timeStr);
    DateTime now = DateTime.now();
    Duration duration = now.difference(time);
    if(duration.inDays == 0){
      result = true;
    }
    return result;
  }

}